#!/bin/sh

# Slackware build script for gtkspell - requiered by sylpheed for spell checking

# Written by (Boricua) (boricua at despiertapr dot com)

#  license :  public domain

# Exit on most errors
set -e

PRGNAM=gtkspell		# replace with name of program
VERSION=2.0.11		# replace with version of program
ARCH=${ARCH:-i686}	# this should not change
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}	# the "_SBo" is required
CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}	# For consistency's sake, use this
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}	# Drop the package in /tmp

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar -xzvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
chmod -R u+w,go+r-w,a-s .


# Your application will probably need different configure flags;
# these are provided as an example only. 
# Be sure to build only shared libraries unless there's some need for
# static.
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --localstatedir=/var

# Compile the application and install it into the $PKG directory
make
make install DESTDIR=$PKG

# Strip binaries and libraries - this can be done with 'make install-strip'
# in many source trees, and that's preferable if so, but if not, use this
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Compress man pages
# For kde programs, you'll need to do this for /opt/kde/share/man instead
# If the man pages are installed to /usr/share/man instead, you'll need to either
# add the --mandir=/usr/man flag to configure or move them manually after the
# make install process is run.
#( cd $PKG/usr/man
#  find . -type f -exec gzip -9 {} \;
#  for i in $(find . -type l) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
#)

# Compress info pages and remove the package's dir file
# If no info pages are installed by the software, don't leave this in the script
#rm -f $PKG/usr/info/dir
#gzip -9 $PKG/usr/info/*.info*

# Remove perllocal.pod and other special files that don't need to be installed,
# as they will overwrite what's already on the system.  If this is not needed, 
# remove it from the script.
( cd $PKG
  # Remove 'special' files
  find . -name perllocal.pod \
    -o -name ".packlist" \
    -o -name "*.bs" \
    | xargs rm -f
)

# Copy program documentation into the package
# The included documentation varies from one application to another, so be sure
# to adjust your script as needed
# Also, include the SlackBuild script in the documentation directory
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
#cp -a <documentation> $PKG/usr/doc/$PRGNAM-$VERSION
#cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# Copy the slack-desc (and a custom doinst.sh if necessary) into ./install
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Make the package; be sure to leave it in $OUTPUT
# If package symlinks need to be created during install *before*
# your custom contents of doinst.sh runs, then add the -p switch to
# the makepkg command below -- see makepkg(8) for details
cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
